package com.maotai.llzhou.timetask;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotai.llzhou.bean.MaotaiWebCookie;
import com.maotai.llzhou.service.MaotaiService;
import com.maotai.llzhou.thread.MaotaiCheckThread;
import com.maotai.llzhou.util.IPFactory;
import com.maotai.llzhou.util.JSUtil;

/**
 * 登录并且抢购
 */
public class MaotaiCheckTask {

	@Resource(name = "maotaiService")
	private MaotaiService maotaiService;

	public void beginTask() {
		List<Map<String, Object>> list;
//		List<Map<String, Object>> addressList;
		try {
			list = maotaiService.getZhanghaoCheck();
//			addressList = maotaiService.getAddressLibrary();
			if (list != null && list.size() > 0) {
//				JSONObject jObject = IPFactory.getKdByUrl(IPFactory.ZM_IP_HTTPS_URL_20);
//				JSONObject data = jObject.getJSONObject("data");
				//{"msg": "", "code": 0, "data": {"count": 10, "proxy_list": ["124.172.117.189:19812", "219.133.31.120:26947", "183.237.194.145:28436", "183.62.172.50:23485", "163.125.157.243:17503", "183.57.42.79:26483", "202.103.150.70:17251", "182.254.129.124:15395", "58.251.132.181:20659", "112.95.241.76:21948"]}}
//				JSONArray jArray = data.getJSONArray("data");
//				JSONObject jObject = IPFactory.getKdByUrl(IPFactory.ZM_IP_HTTPS_URL_200);
//				JSONObject jObject = IPFactory.getKdByUrl(IPFactory.XM_IP_HTTP_URL_50);
//				JSONObject jObject = IPFactory.getKdByUrl(IPFactory.JL_IP_HTTP_URL_50);
//				
//				JSONArray jArray = jObject.getJSONArray("data");
				JSONArray jArray = IPFactory.getIpAndPort();
				int count = list.size();
				if(jArray.size()<count){
					count=jArray.size();
				}
//				if(addressList.size()<count){
//					count=addressList.size();
//				}
				for(int i=0;i<count;i++){
					Map<String, Object> zhanghao = list.get(i);
//					Map<String, Object> addrMap = addressList.get(i);
					JSONObject jj = (JSONObject) jArray.get(i);
					String ipAndPort = jj.getString("ip")+":"+jj.getString("port");
//					String ipAndPort = jj.getString("IP")+":"+jj.getString("Port");
//					String ipAndPort = IPFactory.getIpByQuanWang().trim();
					zhanghao.put("checkstatus", "1");
					maotaiService.updateZhanghaoCheckStatus(zhanghao);
//					addrMap.put("usestatus", "1");
//					maotaiService.updateAddressLibraryStatus(addrMap);
					MaotaiWebCookie mwc = new MaotaiWebCookie();
					String kcross = zhanghao.get("kcross")+"";
					String acw_tc = zhanghao.get("acw_tc")+"";
					String csrfToken = zhanghao.get("csrfToken")+"";
					String auth = zhanghao.get("auth")+"";
					mwc.setKcross(kcross);
					mwc.setAcw_tc(acw_tc);
					mwc.setCsrfToken(csrfToken);
					mwc.setAuth(auth);
					String timestamp = System.currentTimeMillis()+"";
					String sign = JSUtil.excuFunctionA(mwc.getAuth()+timestamp);
					mwc.setSign(sign);
					mwc.setTimestamp(timestamp);
					MaotaiCheckThread myyt = new MaotaiCheckThread(maotaiService,zhanghao,ipAndPort,mwc);
//					MaotaiCheckThread92 myyt = new MaotaiCheckThread92(maotaiService,zhanghao,ipAndPort,mwc);
					myyt.start();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
